package com.ls.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ls.dto.DepartmentDto;
import com.ls.mapper.DepartmentMapper;
import com.ls.pojo.Department;
import com.ls.service.DepartmentService;
import com.ls.utils.Status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     * 修改部门状态
     * @param status
     * @return
     */
    @Override
    public Boolean updateStatus(Integer dep_id,Integer status) {

        if (status==0){
            status = 1;
        }else {
            status = 0;
        }

        System.out.println("修改后id："+dep_id+"/t status："+status);
        try {
            departmentMapper.updateStatus(dep_id,status);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 修改部门信息
     * @param departmentDto
     * @return
     */
    @Override
    public Boolean update(DepartmentDto departmentDto) {

        try {
            departmentMapper.update(departmentDto);
            System.out.println(departmentDto);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 添加部门信息
     * @param departmentDto
     * @return
     */
    @Override
    public Boolean insert(DepartmentDto departmentDto) {
        try {
            departmentMapper.insert(departmentDto);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 根据id获取部门信息(结果集)
     * @param depId
     * @return
     */
    @Override
    public Status getDepartmentResult(Integer depId) {

        try {
            return new Status()
                    .setStatus(true)
                    .setMsg(departmentMapper.getDepartment(depId));
        } catch (Exception e) {
            return new Status().setStatus(false).setMsg("获取失败~");
        }
    }

    /**
     * 获取分页信息(结果集)
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Status get(Integer page, Integer pageSize) {

        PageHelper.startPage(page,pageSize);

        PageInfo deps = new PageInfo(departmentMapper.get());

        try {
            return new Status().setStatus(false).setMsg(deps);
        } catch (Exception e) {
            return new Status().setStatus(false).setMsg("获取失败~");
        }

    }

    /**
     * 获取赛选后的分页信息(结果集)(名称)
     * @param content
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Status getOfName(String content,Integer page, Integer pageSize) {

        PageHelper.startPage(page,pageSize);

        PageInfo deps = new PageInfo(departmentMapper.getOfName(content));

        try {
            return new Status().setStatus(true).setMsg(deps);
        } catch (Exception e) {
            return new Status().setStatus(false).setMsg("获取失败~");
        }
    }

    /**
     * 获取赛选后的分页信息(结果集)(地址)
     * @param content
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Status getOfAddress(String content, Integer page, Integer pageSize) {

        PageHelper.startPage(page,pageSize);

        PageInfo deps = new PageInfo(departmentMapper.getOfAddress(content));

        try {
            return new Status().setStatus(true).setMsg(deps);
        } catch (Exception e) {
            return new Status().setStatus(false).setMsg("获取失败~");
        }
    }

    /**
     * 获取部门状态为。。的所有信息
     * @param status
     * @return
     */
    @Override
    public Status getOfStatus(Integer status,Integer page,Integer pageSize) {

        PageHelper.startPage(page,pageSize);

        PageInfo deps = new PageInfo(departmentMapper.getOfStatus(status));

        try {
            return new Status().setStatus(true).setMsg(deps);
        } catch (Exception e) {
            return new Status().setStatus(false).setMsg("操作失败");
        }
    }

    /**
     * 修改部门状态(结果集)
     * @param dep_id
     * @param status
     * @return
     */
    @Override
    public Status updateStatusResult(Integer dep_id,Integer status) {

        if (updateStatus(dep_id,status)){
            return new Status().setStatus(true).setMsg("操作成功~");
        }
        return new Status().setStatus(false).setMsg("操作失败~");
    }

    /**
     * 修改部门信息(结果集)
     * @param departmentDto
     * @return
     */
    @Override
    public Status updateResult(DepartmentDto departmentDto) {

        if (update(departmentDto)){
            return new Status().setStatus(true).setMsg("操作成功~");
        }
        return new Status().setStatus(false).setMsg("操作失败~");
    }

    /**
     * 添加部门信息(结果集)
     * @param departmentDto
     * @return
     */
    @Override
    public Status insertResult(DepartmentDto departmentDto) {
        if (insert(departmentDto)){
            return new Status().setStatus(true).setMsg("操作成功~");
        }
        return new Status().setStatus(false).setMsg("操作失败~");
    }

    /**
     * 获取部门列表
     * @return
     */
    @Override
    public Status getDepartmentResult() {
        try {
            return new Status().setStatus(true).setMsg(departmentMapper.getDepartment());
        } catch (Exception e) {
            return new Status().setStatus(false).setMsg("操作失败");
        }
    }

    @Override
    public Integer getDepId(String departmentName) {
        return departmentMapper.getDepId(departmentName);
    }

}
